import query from '@dfeidao/atom-nodejs/db/query';

interface Message {
	productid: string;
	client_type: string;
}

interface D {
	formid: string;
	action_name: string;
	code: string;
	parent_no: string;
	menu_no: string;
	menu_name: string; code_no: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const { prepare, exec } = query(action_id, session_id, systemid);
	prepare('pub_menu', ['_id', 'menu_no',
		'menu_name', 'formid', 'parent_no', 'productid'], { productid: msg.productid, client_type: msg.client_type }, 199, 1, [], []);
	const [data] = await exec<D>();
	const res = new Array();
	data.forEach((item) => {
		if (item.formid == null) {
			item.formid = "";
		}
		res.push(item.formid);
	});
	const rey = new Array();
	res.forEach((item) => {
		if (item) {
			rey.push(item);
		}
	});


	const data1 = new Array();
	data.forEach((item) => {
		if (item.formid == null) {
			item.formid = "";
		}
		data1.push(item);
	});

	prepare('pub_form_code', ['_id', 'code',
		'action_keyword', 'action_name', 'formid'], { productid: msg.productid, formid: { $in: rey } }, 199, 1, [], []);
	const [ret] = await exec<D>();
	data1.forEach((item) => {
		ret.forEach((it) => {
			if (item.formid === it.formid) {
				it.parent_no = item.menu_no,
					it.menu_no = it.code + it.formid,
					it.menu_name = it.action_name,
					it.code_no = it.code;
			}
		});
	});
	const data2 = data1.concat(ret);
	return data2;
}
